package com.fs.repository;

import com.fs.entity.Customer;
import com.fs.entity.PurchaseRecord;
import com.fs.entity.PurchaseState;
import com.fs.entity.Room;
import org.springframework.data.jpa.repository.*;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

public interface PurchaseRepository extends JpaRepository<PurchaseRecord, Integer>, JpaSpecificationExecutor<PurchaseRecord> {
    Integer countByRoom(Room room);

    @Query(value = "select * from purchase_record where room_id = ?1 and " +
            "date(begin_time) = current_date() and date(end_time) = current_date()", nativeQuery = true)
    List<PurchaseRecord> findPurchaseOfTodayByRoom(int roomId);

    @Query(value = "select * from purchase_record where room_id = ?1 and " +
            "date(begin_time) = ?2 and date(end_time)= ?2", nativeQuery = true)
    List<PurchaseRecord> findPurchaseByRoomAndDate(int roomId, String date);

    @Modifying
    @Query("update PurchaseRecord set payingLock= ?2 where id=?1")
    int updatePayingLockById(int id, Boolean lock);

    @Modifying
    @Query("update PurchaseRecord set payingLock= ?2 where tradeNo = ?1")
    int updatePayingLockByTradeNo(String tradeNo, Boolean lock);

    @Modifying
    @Query("update PurchaseRecord set state = ?2 where id=?1")
    int updatePurchaseState(int id, PurchaseState state);

    @Query(value = "select * from purchase_record where room_id in(?1) and customer_id in (?2) " +
            "and date(begin_time)=current_date() and date(end_time)=current_date()", nativeQuery = true)
    List<PurchaseRecord> findRecordByCustomersAndRoomsOfToday(Collection<Integer> roomIds, Collection<Integer> customerIds);

    PurchaseRecord findByTradeNo(String tradeNo);

    List<PurchaseRecord> findAllByPayingLockIsTrue();


}
